package com.chinasoftosg.sicap.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;

import com.chinasoftosg.sicap.pojo.AdminInfo;
import com.chinasoftosg.sicap.pojo.Rights;
import com.chinasoftosg.sicap.utils.bean.AdminInfoBean;
import com.chinasoftosg.sicap.utils.bean.ZTreeBean;

public class CommonUtil
{
    
    //获取项目更目录
    public static String getBasePath(HttpServletRequest request)
    {
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://" + request.getServerName()
                + ":" + request.getServerPort() + path + "/";
        return basePath;
    }
    
    /**
     * 
     * Description: <br>
     * 1、…<br>
     * 2、…<br>
     * Implement: <br>
     * 1、…<br>
     * 2、…<br>
     * 
     * @param session
     * @return 
     * @see
     */
    public static String getNavTree(HttpSession session)
    {
        List<ZTreeBean> zTreeBeans = new ArrayList<ZTreeBean>();
        AdminInfoBean adminInfoBean = (AdminInfoBean)session.getAttribute(ConstantUtil.ADMININFO_BEAN);
        if (CommonUtil.isEmpty(adminInfoBean))
        {
            return JSONArray.fromObject(zTreeBeans).toString();
        }
        
        List<Rights> list = adminInfoBean.getRights();
        if (CommonUtil.isEmpty(list))
        {
            return JSONArray.fromObject(zTreeBeans).toString();
        }
        
        for (int i = 0; i < list.size(); i++)
        {
            Rights rights = list.get(i);
            ZTreeBean tree = new ZTreeBean();
            tree.setId(rights.getRightsNo());
            tree.setpId(rights.getRightsPNo());
            tree.setName(rights.getRightsName());
            tree.setUrl(rights.getRightsURL());
            tree.setTarget("main");
            tree.setParent(rights.isRightsIsParent());
            zTreeBeans.add(tree);
        }
        return JSONArray.fromObject(zTreeBeans).toString();
    }
    
    /**
     * 判断对象是否为空对象
     * 
     * @param obj
     * @return
     */
    public static boolean isEmpty(Object obj)
    {
        if (null == obj)
        {
            return true;
        }
        return false;
    }
    
    /**
     * 判断集合是否为空
     * 
     * @param list
     * @return
     */
    public static boolean isEmpty(List<?> list)
    {
        if (null != list && list.size() > 0)
        {
            return false;
        }
        return true;
    }
    
    public static boolean isEmpty(Map<?, ?> map)
    {
        if (null != map && map.size() > 0)
        {
            return false;
        }
        return true;
    }
}
